﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// 根据品种编号搜索所有需求，并自动进行累计
    /// 选择进入需要创建采购单或生产单队列，
    /// 创建生产单
    /// 创建留位关系
    /// </summary>
    public partial class TR2EKWD02
    { 
        public TR2EKWD02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWDECK();
            DataContext = MV;
            InitializeComponent();
         } 
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                ABAUF = "";
                return;
            } 
            ABAUF = dwMaster["WDAUFKRS"].ToString() + dwMaster["WDAUFLFD"].ToString() + dwMaster["WDAUFPOS"].ToString() + dwMaster["WDUPOS"].ToString() + "VA ";

        }  
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                dwData = TBMain.Clone().NewRow();
                dwData["WDMANDANT"] = app.Mandant;
                dwData["WDSA"] = WDSA;
                dwData["WDTYP"] = WZTYP;
                dwData["WDWAKZ"] =ARTST;
                dwData["WDART"] = ART.Trim();
                dwData["WDFARBE"] = FARBE.Trim();
                dwData["WDAUFKRS"] = " AND WDAUFKRS LIKE '%" + KRS.Trim() + "%'";
                dwData["WDAUFLFD"] = " AND WDAUFLFD LIKE '%" + LFD.Trim() + "%'";
                dwData["WDAUFPOS"] = " AND WDAUFPOS LIKE '%" + POS.Trim() + "%'"; 
                if(WZTYP=="N")
                {
                    dwData["WDKUNDNR"] = " AND WDZUGET + WDRESERV < WDBEDARF";
                }
                else
                {
                    dwData["WDKUNDNR"] = "";
                }
                dwData["WDBEDST"] = " AND ABPAUFART='" + AUFART + "'";
                SResult = TMV.QWD25REK(dwData);
                MergeData(SResult, false);   
                base.QueryCommand(sender, e);
                TBDetail.Clear();
                TBDetail.Merge(TBMain.Clone());
                vwDetail.Source = TBDetail;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (CLSLIEFNR == "")
            {
                CLSLIEFNR = dwMaster["LSLIEFNR"].ToString();
            }
            else
            {
                if (CLSLIEFNR != dwMaster["LSLIEFNR"].ToString())
                {
                    MessageBox.Show(CLSLIEFNR + "供应商不同!");
                    return;
                }
            }

            filter = "WDAUFKRS='" + dwMaster["WDAUFKRS"].ToString() + "' AND WDAUFLFD='" + dwMaster["WDAUFLFD"].ToString() + "' AND WDAUFPOS='" + dwMaster["WDAUFPOS"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                dwData = TBDetail.NewRow();
                dwData.ItemArray = row.ItemArray;
                TBDetail.Rows.InsertAt(dwData, 0);
                row.Delete();
            }
            TBMain.AcceptChanges();
            TBDetail.AcceptChanges();
        }
       

        public void ByLSCommand(object sender, RoutedEventArgs e)
        {
            if (dwWD == null)
                return;
            DataSet Set = new DataSet();
            filter = "LSLIEFNR='" + dwWD["LSLIEFNR"].ToString() + "'";
            rows = TBMain.Select(filter);
            if (rows.Length == 0)
                return;
            DataTable tb = rows.CopyToDataTable();
            TBDetail.Merge(tb);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            vwMaster.View.MoveCurrentToFirst();
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwDetail == null)
                return;
            filter = "WDAUFKRS='" + dwDetail["WDAUFKRS"].ToString() + "' AND WDAUFLFD='" + dwDetail["WDAUFLFD"].ToString() + "' AND WDAUFPOS='" + dwDetail["WDAUFPOS"].ToString() + "'";
            rows = TBDetail.Select(filter);
            vwDetail.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = row.ItemArray;
                TBMain.Rows.InsertAt(dwData, 0);
                row.Delete();
            }
            TBMain.AcceptChanges();
            TBDetail.AcceptChanges();
            base.DeleteCommand(sender, e);
        } 
    }
}
